package com.ymatou.iapi.delivery.testcase;

import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.delivery.parameter.ModifyBillNoRuleReq;
import com.ymatou.iapi.delivery.service.ModifyBillNoRuleCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.BillnoRule;
import com.ymttest.database.sqlwapper.BillnoRuleWapper;
import com.ymttest.utils.TestDataManager;
/**
*修改拦截规则接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_modifyBillNoRule {
	private static ModifyBillNoRuleReq bean;
	private static ModifyBillNoRuleCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改拦截规则接口");
	}

	@Before
	public void caseUp() {
		bean = new ModifyBillNoRuleReq();
		call = new ModifyBillNoRuleCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_modifyBillNoRule_000() {
		Logger.start(true, "test");
		try {

			Logger.info("pattern:" + Pattern.compile(".*sss"));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_modifyBillNoRule_001() {
		Logger.start(true, "修改一个不存在的物流拦截,数据库插入新物流规则成功");
		try {

			bean.setDescription("autotest");
			bean.setEnabled(true);
			bean.setExpressName("洋码头自动化测试");
			bean.setObservationMode(true);
			bean.setOldRegexRule(".*");
			bean.setRegexRule(String.format(".*#%s",
					TestDataManager.getRandomNum(5)));

			bean.setYmtExpressCode(String.format("autotest%s",
					TestDataManager.getRandomNum(5)));

			call.setData(bean);
			call.callService();

			List<BillnoRule> rules = new BillnoRuleWapper().selectBillnoRule(
					bean.getYmtExpressCode(), bean.getRegexRule());

			BillnoRule billnoRule = rules.get(0);

			Logger.verifyEquals(bean.getDescription(),
					billnoRule.getDescription(), "校验Description");

			Logger.verifyEquals(bean.isEnabled(), billnoRule.getEnabled(),
					"校验Enabled");
			Logger.verifyEquals(bean.isObservationMode(),
					billnoRule.getObservation_mode(), "校验Observation_mode");

			Logger.verifyEquals(bean.getExpressName(),
					billnoRule.getExpress_name(), "校验ExpressName");

			Logger.verifyEquals(bean.getYmtExpressCode().toUpperCase(),
					billnoRule.getYmt_expresscode(), "校验YmtExpressCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_modifyBillNoRule_002() {
		Logger.start(true, "修改一个存在的物流拦截规则,数据库更新物流规则成功");
		try {

			BillnoRuleWapper billnoRuleWapper = new BillnoRuleWapper();

			List<BillnoRule> rules = billnoRuleWapper.selectBillnoRule(
					"AUTOTEST%", null);

			BillnoRule billnoRule = rules
					.get(new Random().nextInt(rules.size()));

			bean.setDescription("autotest");
			bean.setEnabled(true);
			bean.setExpressName(billnoRule.getExpress_name());
			bean.setObservationMode(true);
			bean.setOldRegexRule(billnoRule.getRegex_rule());
			bean.setRegexRule(String.format(".*%s",
					TestDataManager.getRandomNum(3)));

			bean.setYmtExpressCode(billnoRule.getYmt_expresscode());

			call.setData(bean);
			call.callService();

			List<BillnoRule> oldRules = new BillnoRuleWapper()
					.selectBillnoRule(bean.getYmtExpressCode(),
							bean.getOldRegexRule());

			List<BillnoRule> newRules = new BillnoRuleWapper()
					.selectBillnoRule(bean.getYmtExpressCode(),
							bean.getRegexRule());

			Logger.verifyIsNull(oldRules, "校验老记录规则不存在");

			BillnoRule newBillnoRule = newRules.get(0);

			Logger.verifyEquals(bean.getDescription(),
					billnoRule.getDescription(), "校验Description");

			Logger.verifyEquals(bean.isEnabled(), billnoRule.getEnabled(),
					"校验Enabled");
			Logger.verifyEquals(bean.isObservationMode(),
					billnoRule.getObservation_mode(), "校验Observation_mode");

			Logger.verifyEquals(bean.getExpressName(),
					billnoRule.getExpress_name(), "校验ExpressName");

			Logger.verifyEquals(bean.getYmtExpressCode().toUpperCase(),
					billnoRule.getYmt_expresscode(), "校验YmtExpressCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_modifyBillNoRule_003() {
		Logger.start(true, "ymtExpressCode 为null");
		try {

			bean.setDescription("autotest");
			bean.setEnabled(true);
			bean.setExpressName("洋码头自动化测试");
			bean.setObservationMode(true);
			bean.setOldRegexRule(".*");
			bean.setRegexRule(String.format(".*#%s",
					TestDataManager.getRandomNum(5)));

			/*			bean.setYmtExpressCode(String.format("autotest%s",
								TestDataManager.getRandomNum(5)));*/

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ILLEGAL_ARGUMENT",
					call.getString("errorCode"), "校验errorCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_modifyBillNoRule_004() {
		Logger.start(true, "regexRule 为null");
		try {

			bean.setDescription("autotest");
			bean.setEnabled(true);
			bean.setExpressName("洋码头自动化测试");
			bean.setObservationMode(true);
			bean.setOldRegexRule(".*");
			/*			bean.setRegexRule(String.format("*#%s",
								TestDataManager.getRandomNum(5)));*/

			bean.setYmtExpressCode(String.format("autotest%s",
					TestDataManager.getRandomNum(5)));

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ILLEGAL_ARGUMENT",
					call.getString("errorCode"), "校验errorCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_modifyBillNoRule_005() {
		Logger.start(true, "oldRegexRule 为null");
		try {

			bean.setDescription("autotest");
			bean.setEnabled(true);
			bean.setExpressName("洋码头自动化测试");
			bean.setObservationMode(true);
			/*			bean.setOldRegexRule(".*");*/
			bean.setRegexRule(String.format("*#%s",
					TestDataManager.getRandomNum(5)));

			bean.setYmtExpressCode(String.format("autotest%s",
					TestDataManager.getRandomNum(5)));

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("ILLEGAL_ARGUMENT",
					call.getString("errorCode"), "校验errorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}